package com.mycompany.j2eem.service.wucyservice;

import com.mycompany.j2eem.Entity.FileUtils;
import com.mycompany.j2eem.Entity.JsonResult;
import com.mycompany.j2eem.Entity.PersonalInformation;
import com.mycompany.j2eem.Entity.ResultCode;
import com.mycompany.j2eem.Mapper.wcymapper.LoginMapper;
import com.mycompany.j2eem.service.common.Ftp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @author asus
 */
@Service
public class StudentService {

    @Autowired
    LoginMapper loginmapper;

    //获取Account_id
    public String getAccountId(String username) {
        String Account_id = loginmapper.getAccountId(username);
        return Account_id;
    }

    //获取个人信息
    public JsonResult getPersonalInformation(String username) {
        String Account_id = getAccountId(username);
        PersonalInformation personalInformation = loginmapper.getInformation(Account_id);
        return new JsonResult(ResultCode.SUCCESS, personalInformation);
    }

    //获取个人信息
    public JsonResult getOthPersonalInformation(String id) {
        //String Account_id = getAccountId(username);
        PersonalInformation personalInformation = loginmapper.getInformationById(id);
        return new JsonResult(ResultCode.SUCCESS, personalInformation);
    }

    //更改学生信息
    public JsonResult updatePersonalInformation(String birthday, String sex, String nickname, String username) {
        String Account_id = getAccountId(username);
        int temp = loginmapper.updatePersonalInformation(birthday, nickname, sex, Account_id);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //获取头像
    public boolean setPictureService(String username, MultipartFile file) throws Exception {
        // 要上传的目标文件存放路径
        String Account_id = loginmapper.getAccountId(username);
        List<MultipartFile> mFile = new ArrayList<>();
        mFile.add(file);
        String oldFile = loginmapper.getPicture(Account_id);
        Ftp ftp = new Ftp();
        List<String> temp = ftp.testFtpClient(mFile);
        loginmapper.updatePicture("/images/" + temp.get(0), Account_id, oldFile);
        return true;
    }

    //关注用户
    public JsonResult conner(String username, String conner_name) {
        String id_me = loginmapper.getAccountId(username);
        int temp = loginmapper.connerUser(id_me, conner_name);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //是否关注此人
    public JsonResult isConner(String username, String conner_name) {
        String id_me = loginmapper.getAccountId(username);
        List<String> listId = loginmapper.getList(id_me);
        if (listId.contains(conner_name)) {
            return new JsonResult(ResultCode.FAIL, "", "已经关注此人");
        }
        return new JsonResult(ResultCode.SUCCESS,"","没有关注此人");
    }

    //取消关注 前面是我，后面是对象
    public JsonResult deleteConner(String username, String conner_name) {
        String id_me = loginmapper.getAccountId(username);
//        String id_you = loginmapper.getAccountId(conner_name);
        int temp = loginmapper.deleteConnerUser(id_me, conner_name);
        return new JsonResult(ResultCode.SUCCESS);
    }

    //获取关注列表
    public JsonResult getListConner(String username) {
        String id = loginmapper.getAccountId(username);
        List<String> listId = loginmapper.getList(id);
        List<PersonalInformation> list = new ArrayList();
        listId.stream().forEach(x -> {
            PersonalInformation temp = loginmapper.getInformation(x);
            list.add(temp);
        });
        return new JsonResult(ResultCode.SUCCESS, list);
    }

    //获取粉丝列表
    public JsonResult getFansList(String username) {
        String id = loginmapper.getAccountId(username);
        List<String> listId = loginmapper.getFansList(id);
        List<PersonalInformation> list = new ArrayList();
        listId.stream().forEach(x -> {
            PersonalInformation temp = loginmapper.getInformation(x);
            list.add(temp);
        });
        return new JsonResult(ResultCode.SUCCESS, list);

    }
}
